﻿package
{
	import debugs.Debug;
	import displays.Sprites.Ivideo;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.text.TextField;
	import media.fixSound;
	import media.sound;
	import net.RXml;
	import net.netvars;
	import flash.events.Event;
	import soundCom.pass;
	import soundCom.QA;
	import soundCom.inLine;
	import soundCom.Scramble;
	import src.libCom.FixLib;
	import gs.TweenMax;
	import src.panel.imagePanel;
	import src.panel.scrollNum;
	import displays.coms.LRCplayer;
	/**
	 * ...
	 * @author keys_star@163.com
	 */
	public class main extends Sprite
	{
		var myqa:QA;
		public var Scramble_mc:Scramble;
		public var money:MovieClip;
		public var pass_mc:pass;
		private var LIB:FixLib;
		public var moneys:Sprite;
		public var Ivideos:Ivideo;
		public var lrcPlay:LRCplayer;
		private var myqs:sound;
		//public var title:imagePanel;
		//public var word:imagePanel;
		public function main() 
		{
			netvars.load(this);
			initLrcPlay();
			initValue();
		}
		public function init() {
			LIB = new FixLib;
			LIB.addEventListener(Event.COMPLETE, libcom);
			pass_mc.addEventListener(Event.SOUND_COMPLETE, wcom);
			pass_mc.addEventListener(Event.CHANGE, lcom);
		}
		public function initVideo() {
			Ivideos.addEventListener(Event.CHANGE, cpChange);
			Ivideos.addEventListener(Event.COMPLETE, cpCom);
		}
		private function cpCom(e:Event):void {
			myqa.init();
			myqa.timerStart();
			pass_mc.init();
			Scramble_mc.init();
			Scramble_mc.systimeStart();
		}
		private function cpChange(e:Event):void {
			lrcPlay.nextTime();
		}
		private function initTitleSound():void {
			myqs = new sound("sound/" +"20.mp3");
			myqs.addEventListener(Event.SOUND_COMPLETE, titleCom);
		}
		private function startTitle():void {
			myqs.play();
		}
		private function titleCom(e:Event):void {
			lrcPlay.init();
			Ivideos.start();
		}
		private function initLrcPlay() {
			lrcPlay = new LRCplayer;
			addChild(lrcPlay);
		}
		private function libcom(e:Event) {
			//LIB.fixNextAnswer();
			var s:String = netvars.getVar("MONEY").toString();
			setMoney(s);
			var id:Number = netvars.getVar("ID");
			var u:String = "QALIB/LIB" + id + "/img/Q.png";
			Debug.send(this + ".imgURL: " + u);
			//word.image = u;
			u = "QALIB/LIB" + id + "/img/title.png";
			//title.image = u;
			initVideo();
			startTitle();
			pass_mc.init();
		}
		public function setMoney(s:String) {
			Debug.send(this + ".money.init: " + s);
			var t = 4 - s.length;
			var t0 = 4 - t;
			var txtArr = new Array;
			var inArr:Array = new Array();
			for (var i = 0; i < t; i++ ) {
				inArr.push(0);
			}
			for (i = 0; i < t0; i++ ) {
				inArr.push(Number(s.charAt(i)));
			}
			var item:Array = new Array;
			item.push(moneys.getChildByName("a") as scrollNum);
			item.push(moneys.getChildByName("b") as scrollNum);
			item.push(moneys.getChildByName("c") as scrollNum);
			item.push(moneys.getChildByName("d") as scrollNum);
			for ( i = 0; i < 4; i++ ) {
				var mc:scrollNum = item[i];
				Debug.send(this + ".num: " + mc);
				mc.start(inArr[i]);
				//mc.start(4);
				var txt = money.getChildByName("txt" + i);
				txt.text = inArr[i];
			}
		}
		public function initValue() {
			var load:RXml = new RXml;
			load.load("config.xml");
			load.addEventListener(Event.COMPLETE, xmlcom);
		}
		private function xmlcom(e:Event) {
			initTitleSound();
			initQA();
			initScra();
			init();
		}
		function initQA() {
			myqa= new QA;
			myqa.addEventListener(Event.CHANGE, qachange);
			myqa.addEventListener(Event.COMPLETE, qacom);
		}
		function initScra() {
			Scramble_mc.init();
			Scramble_mc.addEventListener(Event.COMPLETE, nexta);
			Scramble_mc.addEventListener(Event.CHANGE, SCstart);
		}
		function SCstart(e:Event) {
			//moneyClose(e);
			TweenMax.to(money, 1, { x:512, y:358, scaleX:1, scaleY:1 } );
			TweenMax.to(money, 1, { alpha:0, delay:1 } );
			pass_mc.gotoAndStop(4);
			Debug.send(this + ".Scramble_mc.start");
			myqa.stop();
		}
		function nexta(e:Event) {
			var id = netvars.getVar("LIBID");
			var len = netvars.getVar("LIBLEN");
			Debug.send(this + ".an id: " + id);
			Debug.send(this + ".an len: " + len);
			if (id < len-1) {
				pass_mc.lose();
			}else {
				var hid = netvars.getVar("ID");
				var u:String = "QALIB/LIB" + hid + "/img/A.png";
				Debug.send(this + ".win.imageURL: " + u);
				//word.image = u;
				pass_mc.win();
			}
		}
		private function lcom(e:Event) {
			Debug.send(this + ".lose.com");
			LIB.fixNextAnswer();
			var s:String = netvars.getVar("MONEY").toString();
			Debug.send(this + ".money." + s);
			//setMoney(s);
			//myqa.init();
			//myqa.timerStart();
			//pass_mc.init();
			//Scramble_mc.init();
			//Scramble_mc.systimeStart();
			startTitle();
			pass_mc.gotoAndStop(4);
		}
		private function wcom(e:Event) {
			LIB.fixNextTitle();
		}
		function qachange(e:Event) {
			if (myqa.TYPE == "MoneyPlay") {
				Debug.send(this + "moneyPlay");
				//moneyPlay();
			}else if (myqa.TYPE == "MoneyClose") {
				Debug.send(this + "moneyClose");
				//moneyClose();
			}else if (myqa.TYPE == "MobilePlay") {
				Debug.send(this + "MobilePlay: " + myqa.ID);
				mobilePlay(myqa.ID);
			}
		}
		function moneyPlay() {
			TweenMax.to(money, 1 , { x:stage.stageWidth / 2, y:stage.stageHeight / 2, scaleX:2, scaleY:2 , alpha:1 } );
			pass_mc.gotoAndStop(3);
		}
		function moneyClose() {
			TweenMax.to(money, 1, { x:512, y:358, scaleX:1, scaleY:1 } );
			TweenMax.to(money, 1, { alpha:0, delay:1 } );
			pass_mc.gotoAndStop(4);
		}
		function qacom(e:Event) {
			Debug.send(this + ".sound.com");
			lrcPlay.init();
			Ivideos.start();
			//moneyClose();
		}
		function mobilePlay(id:uint) {
			var mc = getChildByName("ph" + id);
			mc.play();
		}
	}
}